using System.Linq;
using System.Web.Mvc;
using log4net;
using Pixster.Controllers.Helpers;
using Pixster.Models;

namespace Pixster.Controllers
{
    public class AlbumController : Controller
    {
        private static readonly ILog LOG = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        [AcceptVerbs(HttpVerbs.Get), OutputCache(CacheProfile = "Album")]
        public ActionResult Index(string directory, string albumName)
        {
            if (string.IsNullOrEmpty(directory))
            {
                LOG.Info("Directory is null, returning to homepage.");
                return RedirectToAction("Index", "Home");
            }

            if (string.IsNullOrEmpty(albumName))
            {
                LOG.InfoFormat("Album is null, returning to Directory '{0}'.", directory);
                return RedirectToAction("Index", "Directory", directory);
            }

            var sitemap = ManifestHelper.Sitemap;

            if(sitemap == null)
            {
                LOG.Info("Sitemap is null, returning to homepage.");
                return RedirectToAction("Index", "Home");
            }

            var dir = sitemap.Where(d => d.SeoName.Equals(directory)).SingleOrDefault();
            
            if(dir == null || dir.Albums == null)
            {
                LOG.InfoFormat("Cannot find Directory '{0}' or it has no Albums, returning to homepage.", directory);
                return RedirectToAction("Index", "Home");
            }

            var album = dir.Albums.Where(a => a.SeoName.Equals(albumName)).SingleOrDefault();

            if(album == null)
            {
                LOG.InfoFormat("Cannot find Album '{0}', returning to directory.", directory);
                return RedirectToAction("Index", "Directory", directory);
            }

            return View(new PixsterViewModel {Sitemap = sitemap, CurrentDirectory = dir, CurrentAlbum = album});
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult GetPhoto(string directory, string albumName, string photoName)
        {
            return Json(LoadPhoto(directory, albumName, photoName));
        }

        private static JsonPhoto LoadPhoto(string directory, string albumName, string photoName)
        {
            if (string.IsNullOrEmpty(directory))
            {
                LOG.Info("Directory is null, returning to homepage.");
                return null;
            }

            if (string.IsNullOrEmpty(albumName))
            {
                LOG.InfoFormat("Album is null, returning to Directory '{0}'.", directory);
                return null;
            }

            if (string.IsNullOrEmpty(photoName))
            {
                LOG.InfoFormat("Photo is null, returning to Album '{0}'.", albumName);
                return null;
            }

            var sitemap = ManifestHelper.Sitemap;

            if (sitemap == null)
            {
                LOG.Info("Sitemap is null, returning to homepage.");
                return null;
            }

            var dir = sitemap.Where(d => d.SeoName.Equals(directory)).SingleOrDefault();

            if (dir == null || dir.Albums == null)
            {
                LOG.InfoFormat("Cannot find Directory '{0}' or it has no Albums, returning to homepage.", directory);
                return null;
            }

            var album = dir.Albums.Where(a => a.SeoName.Equals(albumName)).SingleOrDefault();

            if (album == null)
            {
                LOG.InfoFormat("Cannot find Album '{0}', returning to directory.", directory);
                return null;
            }

            var photo = album.Photos.Where(p => p.SeoName.Equals(photoName)).SingleOrDefault();

            if(photo == null)
            {
                return null;
            }

            var viewModel = new PixsterViewModel {Sitemap = sitemap, CurrentDirectory = dir, CurrentAlbum = album, CurrentPhoto = photo};

            return new JsonPhoto {
                                     FileName = photo.FileName,
                                     SeoName = photo.SeoName,
                                     Title = photo.Title,
                                     DirectoryPath = photo.DirectoryPath,
                                     Comment = photo.Comment,
                                     DisplayImagePath = photo.DisplayImagePath,
                                     NextPhotoName = viewModel.NextPhoto !=null ? viewModel.NextPhoto.SeoName : "",
                                     PreviousPhotoName = viewModel.PreviousPhoto != null ? viewModel.PreviousPhoto.SeoName : ""
                                 };
        }
    }
}